package com.game.center.respository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.game.center.AbstractDao;
import com.game.center.entity.User;
import com.game.center.mapper.UserMapper;
import com.game.center.redis.EnumRedisKey;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author 林道敬
 * @Date 2023-07-29
 * @Description
 **/
@Component
public class UserRespository extends AbstractDao<User> {
    @Resource
    private UserMapper userMapper;

    public long getNextUserID(){
        long userId = redisTemplate.opsForValue().increment(EnumRedisKey.USER_ID_INCR.getKey());
        return userId;
    }
    @Override
    protected EnumRedisKey getRedisKey() {
        return EnumRedisKey.USER_ACCOUNT;
    }

    @Override
    protected Class<User> getEntityClass() {
        return User.class;
    }

    @Override
    protected BaseMapper<User> getRepository() {
        return userMapper;
    }

    @Override
    protected void saveOrUpdateToDB(User user) {
        if(user != null){
            if(user.getId()==null){
                userMapper.insert(user);
            }else {
                userMapper.updateById(user);
            }
        }
    }
}
